//
//  XJLRegisterPwdViewController.m
//  XJL_WeChat
//
//  Created by XJL on 16/7/31.
//  Copyright © 2016年 Xujiali. All rights reserved.
//

#import "XJLRegisterPwdViewController.h"
#import <ReactiveCocoa/ReactiveCocoa.h>
#import "XJLLoginViewController.h"

@interface XJLRegisterPwdViewController ()
@property (weak, nonatomic) IBOutlet UITextField *pwdField;

@property (weak, nonatomic) IBOutlet UILabel *telNumberLable;
@property (weak, nonatomic) IBOutlet UIButton *submitBtn;

@end

@implementation XJLRegisterPwdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //监听文字长度的改变
    @weakify(self)
    [self.pwdField.rac_textSignal subscribeNext:^(NSString *text) {
        @strongify(self)
        self.submitBtn.enabled = text.length > 0;
    }];
    //显示电话号码
    NSString *text = [NSString stringWithFormat:@"手机号码 %@ %@",_code,_tel];
    self.telNumberLable.text = text;
    
}
- (IBAction)giveup {
    
    [self.presentingViewController.presentingViewController.presentingViewController dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)submit {
    [SVProgressHUD showWithStatus:@"注册中..."];
    [[EaseMob sharedInstance].chatManager asyncRegisterNewAccount:self.tel password:self.pwdField.text withCompletion:^(NSString *username, NSString *password, EMError *error) {
//        error = nil;
        if (!error) {
            [SVProgressHUD showSuccessWithStatus:@"注册成功，将回到登录界面..."];
            //回到登录界面，把“注册账号”传给“登录界面”
            //1、获取登录控制器
            XJLLoginViewController *logVc = (XJLLoginViewController *)self.presentingViewController.presentingViewController.presentingViewController;
            logVc.loginAccount = self.tel;
            //回到登录界面
            [self giveup];
        }else{
            [SVProgressHUD showErrorWithStatus:error.description];
        }
    } onQueue:nil];

}

@end
